package cn.itcast.service.cargo.impl;


import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ContractServiceImpl implements ContractService {


    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ContractProductDao contractProductDao;


    @Autowired
    private ExtCproductDao extCproductDao;
    /*
     购销合同分页
     */
    @Override
    public PageInfo<Contract> findByPage(ContractExample contractExample, int pageNum, int pageSize) {

        //设置页面大小
        PageHelper.startPage(pageNum,pageSize);

        //查询全部
        List<Contract> contractList = contractDao.selectByExample(contractExample);

        //封装PageInfo对象
        PageInfo<Contract> pageInfo = new PageInfo<>(contractList);

        return pageInfo;
    }


    //大区经理查看购销合同
    @Override
    public PageInfo<Contract> findPageDeptId(String companyId, String deptId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Contract> contractList = contractDao.findByDeptId(companyId,deptId);
        return new PageInfo<>(contractList);
    }


    //查询全部
    @Override
    public List<Contract> findAll(ContractExample contractExample) {

        List<Contract> contractList = contractDao.selectByExample(contractExample);

        return contractList;
    }

    //根据主键查询
    @Override
    public Contract findById(String id) {

        Contract contract = contractDao.selectByPrimaryKey(id);
        return contract;
    }


    //保存购销合同
    @Override
    public void save(Contract contract) {
        contract.setId(UUID.randomUUID().toString());
        //购销合同创建时间
        contract.setCreateTime(new Date());
        //购销合同的更新时间
        contract.setUpdateTime(new Date());
        contract.setState(0);
        contractDao.insertSelective(contract);
    }

    //更新购销合同
    @Override
    public void update(Contract contract) {
        //购销合同的更新时间
        contract.setUpdateTime(new Date());
        contractDao.updateByPrimaryKeySelective(contract);
    }

    //删除购销合同
    @Override
    public int delete(String id) {
        //删除合同之前先把合同取出来
        Contract contract = contractDao.selectByPrimaryKey(id);
        Integer state = contract.getState();
        if (state!=1&&state!=2){
            //1删除合同
            contractDao.deleteByPrimaryKey(id);
            System.out.println("deleteByPrimaryKey"+id);
            //2. 删除货物， 一个合同可能有多个货物
            ContractProductExample contractProductExample = new ContractProductExample();
            //添加查询条件，该合同的货物
            contractProductExample.createCriteria().andContractIdEqualTo(id);
            //该合同的所有货物
            List<ContractProduct> contractProductList = contractProductDao.selectByExample(contractProductExample);
            //遍历所有货物
            if (contractProductList!=null){
                for (ContractProduct contractProduct : contractProductList) {
                    String id1 = contractProduct.getId();
                    System.out.println("id1"+id1);
                    ExtCproductExample extCproductExample = new ExtCproductExample();
                    extCproductExample.createCriteria().andContractIdEqualTo(id1);
                    List<ExtCproduct> extCproductList = extCproductDao.selectByExample(extCproductExample);
                    for (ExtCproduct extCproduct : extCproductList) {
                        String id2 = extCproduct.getId();
                        System.out.println("id2"+id2);
                        extCproductDao.deleteByPrimaryKey(id2);
                    }
                    contractProductDao.deleteByPrimaryKey(id1);

                }

            }

        }
        return state;
    }

}
